﻿using System;
using System.Globalization;
using System.Data.Linq;
using System.Linq;

using SBPweb.PlAzureNet.Shared.Library.Aspects;

using SBPweb.PlAzureNet.Server.Data.Actors;

using SBPweb.PlAzureNet.Server.Library.Base;

namespace SBPweb.PlAzureNet.Server.Data.Context
{

    public static class TranslateableExtender
    {

        public static T GetTranslated<T>(this T extendee, DataTablesContext dataContext)
            where T : ITranslateable
        {
            return GetTranslated<T>(extendee, dataContext, ServiceContractBase.GetCurrentContextCulture());
        }

        public static T GetTranslated<T>(this T extendee, DataTablesContext dataContext, CultureInfo culture)
            where T : ITranslateable
        {
            INameTranslateable tN = extendee as INameTranslateable;
            if (tN != null)
            {
                tN.Name = dataContext.Translations.FirstOrDefault(x => x.TermId == tN.NameId && x.CultureCode == culture.Name).Value;
            }

            IDescriptionTranslateable dN = extendee as IDescriptionTranslateable;
            if (dN != null)
            {
                dN.Description = dataContext.Translations.FirstOrDefault(x => x.TermId == dN.DescriptionId && x.CultureCode == culture.Name).Value;
            }

            return extendee;
        }
        
    }

}